package chapter1.No1_8_暂停线程.aSuspend和resume方法的使用;

/**
 * *：suspend() 暂停线程
 * resume() 回复线程执行
 *
 * @author liujian
 * @version 2016/11/9 0009
 */
public class MyThread extends Thread {
    private int i = 0;

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }

    @Override
    public void run() {
        try {
            while (true) {
                Thread.sleep(500);

                if (this.isInterrupted()) {
                    System.out.println("我要退出了~");
                    break;
                }
                i++;
                System.out.println(i);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyThread m1 = new MyThread();
        m1.start();
        Thread.sleep(2000);

        // A段位
        m1.suspend();
        System.out.println("A1：" + m1.getI());
        Thread.sleep(200);
        System.out.println("A2：" + m1.getI());

        // B 段位
        m1.resume();
        Thread.sleep(100);
        System.out.println("B1："+m1.getI());
        Thread.sleep(500);
        System.out.println("B2："+m1.getI());

        System.out.println("END");
        m1.interrupt();
    }
}
